package serverlet;

import Server.StudentServer;
import user.Student;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.sql.SQLException;
import java.util.List;

@WebServlet(name = "ManagerInitServlet", urlPatterns = "/managerinit")
public class ManagerInitServlet extends HttpServlet {
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        try {
            System.out.println("获取数据中。。。");
            StudentServer studentServer = new StudentServer();
            List<Student> students=null;
            String start = "0",end = "3";
            int count = studentServer.countStudent();
            if(null!=(request.getParameter("start"))){
                start = request.getParameter("start");
                if(Integer.parseInt(request.getParameter("start"))>((count-1) / 3)){
                    start=Integer.toString(Integer.parseInt(request.getParameter("start"))-1);

                }else if(Integer.parseInt(request.getParameter("start"))<0){
                    start="0";
                }
                System.out.println("已经执行");
            }
            students = studentServer.getStudentServer(start,end);
            request.getSession().setAttribute("list", students);
            request.getSession().setAttribute("count", count);
            request.getSession().setAttribute("pagenum", ((count-1) / 3));
            request.getSession().setAttribute("start", Integer.parseInt(start));
            response.sendRedirect("manager.jsp");
            System.out.println("数据获取完毕");
            System.out.println(students);
//            System.out.println("start="+start);
//            System.out.println("网页start:"+request.getParameter("start"));
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
    }

    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        this.doPost(request,response);
    }
}
